<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/9
 * Time: 16:52
 */

namespace app\api\controller;


class Dev
{
    /**
     *
     * 绑定设备
     * http://127.0.0.1:9093/api/user/devBind
     *
     */
    public function devBind()
    {
        $currDate = getCurrDate();
        $auth = trim(input("request.auth"));//用户auth
        $devtype = trim(input("request.devtype"));//设备类型 0睡眠仪 1枕头
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }


        $devsn = trim(input("request.devsn"));//--设备编号
        $devname = trim(input("request.devname"));//--设备名字
        $mac = trim(input("request.mac"));  //--设备mac

        if ($devtype != 0 && $devtype != 1 &&  $devtype != 88) {
            $return = ['err' => 2, 'msg' => '操作失败,设备类型不正确', 'devMsg' => '操作失败，参数错误[devtype]'];
            return json($return);
        }

        $DevModel = new \app\admin\model\Dev();
        $w = array(
            'uid' => $userInfo['id'],
            'devtype' => $devtype,
            'devname' => $devname,
            'is_delete' => 0
        );
        $data = array(
            'uid' => $userInfo['id'],
            'devsn' => $devsn,
            'devname' => $devname,
            'devtype' => $devtype,
            'mac' => $mac,
            'ctime' => $currDate['date'],
            'is_delete' => 0
        );
        $devInfo = $DevModel->getDataForWhere($w);
        if (!empty($devInfo)) {//修改
            $res = $DevModel->updateData($data, $devInfo['id']);
            if ($res || $res == 0) {
                $return['err'] = 0;
                $return['msg'] = '操作成功';
                $return['id'] = $devInfo['id'];
                return json($return);
            } else {
                $return['err'] = 3;
                $return['msg'] = '操作失败';
                return json($return);
            }

        } else//添加
        {
            $insertID = $DevModel->add($data);
            if ($insertID) {
                $return['err'] = 0;
                $return['msg'] = '操作成功';
                $return['id'] = $insertID;
                return json($return);

            } else {
                $return['err'] = 4;
                $return['msg'] = '操作失败';
                return json($return);
            }
        }
    }

    /**
     *
     * 解除绑定设备
     * http://127.0.0.1:9093/api/user/devUnBind
     *
     */
    public function devUnBind()
    {
        $currDate = getCurrDate();
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $id = trim(input("request.id"));

        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[id]'];
            return json($return);
        }
        $DevModel = new \app\admin\model\Dev();
        $devInfo = $DevModel->getDataForWhere(array('uid' => $userInfo['id'], 'id' => $id, 'is_delete' => 0));
        if (empty($devInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,设备已经不存在了', 'devMsg' => '操作失败，参数错误[id]'];
            return json($return);
        }
        $update = array('is_delete' => 1, 'delete_time' => $currDate['date']);
        $w = array('uid' => $userInfo['id'], 'id' => $id);

        $res = $DevModel->updateDataForWhere($update, $w);
        if ($res || $res == 0) {
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }

    /**
     *
     * 获取我的设备列表接口
     * http://127.0.0.1:9093/api/user/devUnBind
     *
     */
    public function lists()
    {
        $currDate = getCurrDate();
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }


        $DevModel = new \app\admin\model\Dev();
        $lists = $DevModel->getDataForWheres(array('uid' => $userInfo['id'], 'is_delete' => 0), "id,uid,devtype,devsn,devname,mac");
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $lists;
        return json($return);

    }


    /**
     *
     * 获取我的设备列表接口
     * http://127.0.0.1:9093/api/user/devUnBind
     *
     */
    public function getInfo()
    {
        $auth = trim(input("request.auth"));//用户auth
        $id = trim(input("request.id"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败,参数错误', 'devMsg' => '操作失败，参数错误[id]'];
            return json($return);
        }


        $DevModel = new \app\admin\model\Dev();
        $info = $DevModel->getDataForWhere(array('uid' => $userInfo['id'], 'id' => $id), "id,uid,devtype,devsn,devname,mac");
        if (empty($info)) {
            $return = ['err' => 1, 'msg' => '设备不存在', 'devMsg' => '设备不存在'];
            return json($return);
        }
        if ($info['is_delete'] == 1) {
            $return = ['err' => 1, 'msg' => '设备已删除', 'devMsg' => '设备已删除'];
            return json($return);
        }
        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $info;
        return json($return);

    }
}